package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.menggudf.commom.CommonUtils;
import com.menggudf.dao.SystemAdminDao;
import com.menggudf.entity.SystemAdmin;
import com.menggudf.model.ResultModel;
import com.menggudf.model.SystemAdminModel;
import com.menggudf.service.SystemAdminService;

@Service
public class SystemAdminServiceImpl implements SystemAdminService{
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(SystemAdminServiceImpl.class);


    @Autowired
    SystemAdminDao systemAdminDao;
    
    @Override
    @Transactional(value = "medicalTM", rollbackFor = Exception.class)
    public ResultModel adminRegister(
            String adminName,Short sex,String occupation,
            SystemAdminModel systemAdminModel) {
        SystemAdmin sys = (SystemAdmin) systemAdminDao.findByAdminCode(systemAdminModel.getAdminCode());
        if(sys!=null){
            if(sys.getFlag()!=1){
                return ResultModel.fail("非系统管理员",null);
            }
            SystemAdmin a = new SystemAdmin();
            a.setAdminCode(UUID.randomUUID().toString());
            a.setAdminName(adminName);
            a.setPassword(CommonUtils.GetMD5Code("123456"));
            a.setSex(sex);
            a.setOccupation(occupation);
            a.setFlag(0);
            systemAdminDao.persist(a);
            return ResultModel.success();
        }
        return ResultModel.fail();
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor = Exception.class)
    public void createAdmin() {
        String adminName = "superAdmin";
        SystemAdmin admin = systemAdminDao.findAdmin(adminName);
        if(admin==null){
            SystemAdmin a = new SystemAdmin();
            a.setAdminCode(UUID.randomUUID().toString());
            a.setAdminName(adminName);
            a.setPassword(CommonUtils.GetMD5Code("123456"));
            systemAdminDao.persist(a);
        }
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public ResultModel login(String adminName, String password) {
        SystemAdmin admin = systemAdminDao.findAdmin(adminName);
        if(admin==null){
            return ResultModel.fail("无此管理员！", null);
        }
        if(!CommonUtils.GetMD5Code(password).equalsIgnoreCase(admin.getPassword())){
            return ResultModel.fail("密码不正确！", null);
        }
        SystemAdminModel sysModel = new SystemAdminModel();
        sysModel.setAdminCode(admin.getAdminCode());
        sysModel.setAdminName(admin.getAdminName());
        sysModel.setOccupation(admin.getOccupation());
        sysModel.setSex(admin.getSex());
        sysModel.setFlag(admin.getFlag());
        return ResultModel.success("登录成功！", sysModel);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public String getAuthorByAdminCode(String adminCode) {
        SystemAdmin sys = (SystemAdmin) systemAdminDao.findByAdminCode(adminCode);
        if(sys!=null){
            return sys.getAdminName();
        }else{
            return null;
        }
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getAdminList(String adminName,int pageSize, int offset) {
        Map<String, Object> remap = new HashMap<String, Object>();
        List<SystemAdmin> list = new ArrayList<SystemAdmin>();
        long i = 0;
        if(StringUtils.hasText(adminName)){
            list = systemAdminDao.getAdminList("from SystemAdmin s where s.adminName='"+adminName+"' and s.flag=0",pageSize,offset);
            i = systemAdminDao.getAdminListCount("select count(*) from SystemAdmin s where s.adminName='"+adminName+"' and s.flag=0",pageSize,offset);
        }else{
            list = systemAdminDao.getAdminList("from SystemAdmin s where s.flag=0",pageSize,offset);
            i = systemAdminDao.getAdminListCount("select count(*) from SystemAdmin s where s.flag=0",pageSize,offset);
        }
        remap.put("total", i);
        remap.put("rows", list);
        return remap;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel eidtAdmin(String adminCode,String adminName,Short sex,String occupation) {
        SystemAdmin sys = (SystemAdmin) systemAdminDao.findByAdminCode(adminCode);
        if(sys==null){
            return ResultModel.fail("管理员为空", null);
        }
        if(!CommonUtils.isEmpty(adminName)){
            sys.setAdminName(adminName);
        }
        if(!CommonUtils.isEmpty(occupation)){
            sys.setOccupation(occupation);
        }
        if(!CommonUtils.isEmpty(sex)){
          sys.setSex(sex);   
        }
        systemAdminDao.merge(sys);
        return ResultModel.success("编辑成功", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel deleteAdmin(String adminCode) {
        SystemAdmin sys = (SystemAdmin) systemAdminDao.findByAdminCode(adminCode);
        if(sys.getFlag()==1){
            return ResultModel.fail("这是超级管理员", null);
        }
        try {
            systemAdminDao.remove(sys);
            return ResultModel.success("删除成功", null);
        } catch (Exception e) {
            LOGGER.error("deleteAdmin",e);
        }
        return ResultModel.fail("删除失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel editPwd(String pwd,String oldPwd, String adminCode) {
        SystemAdmin sys = (SystemAdmin) systemAdminDao.findByAdminCode(adminCode);
        if(sys==null){
            return ResultModel.fail("无此管理员", null);
        }
        if(!CommonUtils.GetMD5Code(oldPwd).equalsIgnoreCase(sys.getPassword())){
            return ResultModel.fail("旧密码输入错误", null);
        }
        if(StringUtils.isEmpty(pwd)){
            return ResultModel.fail("密码不能为空", null);
        }
        sys.setPassword(CommonUtils.GetMD5Code(pwd));
        try {
            systemAdminDao.merge(sys);
            return ResultModel.success("修改成功", null);
        } catch (Exception e) {
            LOGGER.error("editPwd fail",e);
        }
        return ResultModel.fail("修改失败", null);
    }

}
